using System;
using System.Collections.Generic;
using System.Text;
using WebService.BusinessEntities;
using WebService.DataAccess;

namespace WebService.BusinessLogic.Managers
{
    /// <summary>
    /// Klasa dostarczajaca funkcjonalnosci w obszarze zarzadzania
    /// zapisem/odczytem historii odbytych gier oraz stanow portfeli graczy
    /// </summary>
    public class GamesHistoryManager
    {
        private String database;
        /// <summary>
        /// Connection-string do bazy danych
        /// </summary>
        public String Database
        {
            get { return database; }
            set { database = value; }
        }
	    
        /// <summary>
        /// Konstruktor parametrowy managera
        /// </summary>
        /// <param name="database">Connection-string do bazy danych</param>
        public GamesHistoryManager(String database)
        {
            Database = database;
        }

        /// <summary>
        /// Funkcja pobiera aktualne saldo konta uzytkownika
        /// </summary>
        /// <param name="user">Uzytkownik</param>
        /// <returns>Stan konta uzytkownika</returns>
        public int GetUserBalance(CasinoUser user)
        {
            List<Wallet> wallets = GetWallets(user);
            int balance = 0;
            int debits = 0;
            int credits = 0;
            foreach (Wallet wal in wallets)
            {
                debits += wal.debit;
                credits += wal.credit;
            }

            balance = debits - credits;
            return balance;
        }

        /// <summary>
        /// Funkcja pobiera wszystkie odbyte w kasynie gry
        /// </summary>
        /// <returns>Lista odbytych wszystkich gier</returns>
        public List<GameHistory> GetAllGamesHistory()
        {
            List<GameHistory> games = new List<GameHistory>();
            GameHistoryRepository rep = new GameHistoryRepository(database);
            games = rep.GetAllGamesHistoryProc();
            return games;
        }

        /// <summary>
        /// Funkcja [po rozdubowie systemu!] pobiera historie oraz
        /// porfele graczy z wszystkich gier
        /// NIEDOSTEPNA poki co - nie ma sensu poki co
        /// </summary>
        /// <returns>Lista wyszystkich gier i stanow kont</returns>
        public List<GameHistoryWallet> GetAllGamesHistoryWallets()
        {
            //tutaj nie ma nic narazie :]
            return new List<GameHistoryWallet>();
        }

        /// <summary>
        /// Funkcja pobiera historie odbytych gier i zawartosci
        /// portfela danego gracza
        /// </summary>
        /// <param name="user">Uzytkownik</param>
        /// <returns>Lista odbytych gier i zawartosc portfela</returns>
        public List<GameHistoryWallet> GetUserGamesHistory(CasinoUser user)
        {
            List<GameHistoryWallet> games = new List<GameHistoryWallet>();
            GameHistoryWalletRepository rep = new GameHistoryWalletRepository(database);
            games = rep.GetUserGamesProc(user.user_id);
            return games;
        }

        /// <summary>
        /// Funkcja pobiera historie portfeli wszystkich graczy
        /// </summary>
        /// <returns>Lista zapisow z portfeli</returns>
        public List<Wallet> GetAllWallets()
        {
            List<Wallet> wallets = new List<Wallet>();
            WalletRepository rep = new WalletRepository(database);
            wallets = rep.GetAllWalletsProc();
            return wallets;
        }

        /// <summary>
        /// Funkcja pobiera historie portfela danego gracza
        /// </summary>
        /// <param name="user">Uzytkownik</param>
        /// <returns>Lista zapisow z portfela</returns>
        public List<Wallet> GetWallets(CasinoUser user)
        {
            List<Wallet> wallets = new List<Wallet>();
            WalletRepository rep = new WalletRepository(database);
            wallets = rep.GetWalletProc(user.user_id);
            return wallets;
        }

        /// <summary>
        /// Funkcja zapisujaca dane dotyczace odbytej gry
        /// </summary>
        /// <param name="game">Encja gry</param>
        /// <param name="users">Identyfikatory grajacych</param>
        /// <param name="wallets">Stan portfeli grajacych po grze</param>
        public void WriteGameResult(GameHistory game, List<CasinoUser> users,
            List<Wallet> wallets)
        {
            GameResultRepository rep = new GameResultRepository(database);
            String ids = "";
            String money = "";
            for (int i = 0; i < users.Count; i++)
            {
                if (i < users.Count - 1)
                {
                    ids = ids + users[i].user_id.ToString() + ";";
                    if (wallets[i].debit > 0)
                    {
                        money = money + wallets[i].debit.ToString() + ";";
                    }
                    else
                    {
                        money = money + "-" + wallets[i].credit.ToString() + ";";
                    }
                }
                else//ostatni rekord - bez srednika juz
                {
                    ids = ids + users[i].user_id.ToString();
                    if (wallets[i].debit > 0)
                    {
                        money = money + wallets[i].debit.ToString();
                    }
                    else
                    {
                        money = money + "-" + wallets[i].credit.ToString();
                    }
                }
            }

            GameResult result = new GameResult(game.completion_date, game.room_name,
                game.start_date, game.type, ids, money);
            
            rep.Add(result);
        }

        /// <summary>
        /// Funkcja dodaje userowi poczatkowa kwote pieniedzy
        /// </summary>
        /// <param name="user">Uzytkownik</param>
        /// <param name="amount">Kwota poczatkowa</param>
        public void GiveInitialMoney(CasinoUser user, int amount)
        {
            GameHistory game = new GameHistory(new DateTime(2000,1,1),-1,"Initial Room",new DateTime(2000,1,1),"R");
            List<CasinoUser> users = new List<CasinoUser>();
            List<Wallet> wallets = new List<Wallet>();
            users.Add(user);
            Wallet wallet = new Wallet();
            wallet.debit = amount;
            wallets.Add(wallet);

            WriteGameResult(game, users, wallets);
        }
    }
}
